package com.boss.oss.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import java.io.IOException;
import java.io.ByteArrayInputStream;

/**
 * 文件上传
 * @author Hu_liantian
 * @date 2020/07/24
 */
public class FileUtil {

    private static String endpoint;

    private static String accessKeyId;

    private static String accessKeySecret;

    static {

        try {
            endpoint = (String) ReadProperty.readProperty().get("endpoint");
            accessKeyId =(String) ReadProperty.readProperty().get("accessKeyId");
            accessKeySecret =(String) ReadProperty.readProperty().get("accessKeySecret");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取oss客户端
     * @return oss
     */
    public static OSS getOss() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 向oss上传文件
     * @param content 文件内容
     * @param ossClient oss客户端
     * @param bucketName bucket名
     * @param objectName 上传到oss的文件名
     * @return 上传结果
     */
    public synchronized static boolean uploadFileToOss(String content, OSS ossClient, String bucketName, String objectName) {
        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
        return true;
    }

}
